# -*- coding: utf-8 -*-            
# @Author : chenliang
# @Time : 2024/10/25 16:31

import pandas as pd
import datetime
from Common.business_common_tool import BusinessCommonTools
from Common.get_sql_object import GetSqlObject
from Common.dingtalk import Ding
from datetime import timedelta
from Common.do_mysql import DoMysql

"""
  获取当天数据
"""
# # 获取当前时间
# current_time = datetime.now()
# # 格式化当前日期为 'YYYY-MM-DD'
# formatted_date = current_time.strftime('%Y-%m-%d')

"""
 获取周
"""
def get_current_week(day=0):
    fmt = lambda x: datetime.datetime.strftime(x, "%Y-%m-%d")
    today = datetime.date.today()
    monday = today - timedelta(days=today.weekday()+day)
    return [fmt(monday + timedelta(days=i)) for i in range(7)]
data_list = get_current_week()
last_week_list = get_current_week(7)


mysql_source = GetSqlObject.get_instance("PRO")
car_list,car_name,amount_list=BusinessCommonTools().get_car_list('PRO',data_list[0],data_list[-1],mysql_source)
query_consignor_sum,query_consignor_list,query_logistics_line,query_logistics_line_today,query_waybill_end_sum,query_waybill_list_today = BusinessCommonTools().get_consignor_and_cooperation_list('PRO',data_list[0],data_list[-1],mysql_source)
query_waybill_list = BusinessCommonTools().get_waybill_list('PRO',last_week_list[0],last_week_list[-1],mysql_source)


#组装成excel数据
df1 = pd.DataFrame(car_list)
df2 = pd.DataFrame(query_consignor_list)
df3 = pd.DataFrame(query_logistics_line_today)
df4 = pd.DataFrame(query_waybill_list_today)
df5 = pd.DataFrame(query_waybill_list)

# 指定要导出的Excel文件名
excel_file = f'/File/{data_list[0]}至{data_list[-1]}.xlsx' # 生产环境文件存放地址
#excel_file = f'File/{data_list[0]}至{data_list[-1]}.xlsx'
excel_file_path = f'http://47.116.122.206{excel_file}'

# 使用ExcelWriter对象
with pd.ExcelWriter(excel_file, engine='xlsxwriter') as writer:
    df1.to_excel(writer, sheet_name='车辆', index=False)
    df2.to_excel(writer, sheet_name='货主', index=False)
    df3.to_excel(writer, sheet_name='线路物流企业', index=False)
    # df4.to_excel(writer, sheet_name=f'交易票{data_list[0]}至{data_list[-1]}', index=False)
    # df5.to_excel(writer, sheet_name=f'交易票{last_week_list[0]}至{last_week_list[-1]}', index=False)



# 车辆
result_string = f'统计本周数据{data_list[0]}至{data_list[-1]}\n'
a1=round(float(amount_list[0]["amount"]),2) # 收入
a2=round(float(amount_list[1]["amount"]),2) # 支出
a3=round((a1-a2),2) # 收益
result_string += f'收入:{a1}  支出:{a2}  收益:{a3}\n'
#result_string = f'截止到{formatted_date}，平台一共新增加已审核通过车辆'+str(car_name[0]['num'])+'。\n'
result_string += "1、新增加已审核通过车辆"+str(len(car_list))+':\n'
if len(car_list) !=0:
    for item in car_list:
        result_string += f" 姓名: {item['name']}, 电话: {item['phone']}, 车牌: {item['license_code']}, 车长: {item['vehicle_length']}\n"
# Ding().ding_hami(result_string)

# 货主
#result_string += f'截止到{formatted_date}，平台一共增加已审核通过货主'+ str(query_consignor_sum[0]['num'])+ '。\n'
result_string += "2、新增加已审核通过货主"+str(len(query_consignor_list))+':\n'
if len(query_consignor_list) !=0:
    for item in query_consignor_list:
        result_string += f" 货主姓名: {item['realName']}, 电话: {item['phone']}, 公司: {item['companyName']}, 统一代码: {item['institutionCode']}\n"

# 线路企业
# result_string += f'截止到{formatted_date}，平台共有线路物流企业'+ str(len(query_logistics_line))+ '。\n'
result_string += "3、新增加线路物流企业"+str(len(query_logistics_line_today))+':\n'
if len(query_logistics_line_today) !=0:
    for item in query_logistics_line_today:
        result_string += f" 货主姓名: {item['realName']}, 电话: {item['phone']}, 公司: {item['orgName']}, 统一代码: {item['orgCode']}\n"

# 线上交易票
#result_string += f'截止到{formatted_date}，平台共有交易运单'+ str(query_waybill_end_sum[0]['num'])+ '。\n'
#result_string += "4、新增加交易票"+str(len(query_waybill_list_today))+':\n'
# if len(query_waybill_list_today) !=0:
#     for item in query_waybill_list_today:
#         result_string += f" 单号: {item['code']}, 货主: {item['realname']}, 电话: {item['phone']}, {item['waybillStatus']},金额: {item['totalFee']}\n"

result_string += f"数据下载地址：{excel_file_path}"
print(result_string)
#Ding().ding_hami(result_string)





